<!DOCTYPE html>
<html lang="en">
  <title>Examples of DataTransfer.{types,items} properties</title>
  <meta content="width=device-width" />
  <style>
    div {
      margin: 0em;
      padding: 2em;
    }
    #target {
      border: 1px solid black;
    }
  </style>

   <style>
        .box {
            background: #cccce5;
            padding: 10px;
            margin: 20px;
        }
        code {
          background: #f5efe2;
          padding: 2px 4px;
          margin: 0 2px;
          border-radius: 3px;
          vertical-align: middle;
        }

    </style>

  <body>
    <h1>
      Examples of <code>DataTransfer</code>. {<code>types</code>,
      <code>items</code>} properties
    </h1>

    <div class="box">
      <h4>可拖拽元素需要设置属性 <code>draggable="true"</code></h4>
      <h4>放置目标区域元素，设置监听事件 <code>ondrop</code> <code>ondragover</code></h4>
    </div>

    <ul>
      <li id="itemFirst" ondragstart="dragstart_handler(event);" draggable="true">
        Drag Item 1 to the Drop Zone
      </li>
      <li id="itemSecond" ondragstart="dragstart_handler(event);" draggable="true">
        Drag Item 2 to the Drop Zone
      </li>
    </ul>

    <div
      id="target"
      ondrop="drop_handler(event);"
      ondragover="dragover_handler(event);"
    >
      Drop Zone
    </div>

    <script>
      function dragstart_handler(ev) {
        console.log("dragStart: target.id = " + ev.target.id);

        // Add this element's id to the drag payload so the drop handler will
        // know which element to add to its tree
        ev.dataTransfer.setData("text/plain", ev.target.id);
        ev.dataTransfer.effectAllowed = "move";
      }

      function drop_handler(ev) {
        console.log("drop: target.id = " + ev.target.id);
        ev.preventDefault();

        // Get the id of the target and add the moved element to the target's DOM
        const data = ev.dataTransfer.getData("text");
        ev.target.appendChild(document.getElementById(data));

        // Print each format type
        for (let i = 0; i < ev.dataTransfer.types.length; i++) {
          console.log(`… types[${i}] = ${ev.dataTransfer.types[i]}`);
        }

        // Print each item's "kind" and "type"
        for (let i = 0; i < ev.dataTransfer.items.length; i++) {
          console.log(
            `… items[${i}].kind = ${ev.dataTransfer.items[i].kind}; type = ${ev.dataTransfer.items[i].type}`
          );
        }
      }

      function dragover_handler(ev) {
        console.log("dragOver");
        ev.preventDefault();
        // Set the dropEffect to move
        ev.dataTransfer.dropEffect = "move";
      }
    </script>
  </body>
</html>
